package com.imooc.dmtest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.SAXException;

import com.imooc.entity.Book;
import com.imooc.handler.SAXParserHandler;
import com.sun.xml.internal.bind.util.AttributesImpl;

public class SAXTest {

	public static void main(String[] args) {
		createXML();
	}

	public static ArrayList<Book> parseXML() {

		// 获取一个SAXParserFactory的实例
		SAXParserFactory factory = SAXParserFactory.newInstance();
		// 通过factory获取SAXParser实例
		SAXParserHandler handler = null;
		try {
			SAXParser parser = factory.newSAXParser();

			handler = new SAXParserHandler();
			parser.parse("src/main/resources/xmlfile/books.xml", handler);
			System.out.println("========共有" + handler.getListBook().size() + "本书");
			for (Book book : handler.getListBook()) {
				System.out.println(book.toString());
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return handler.getListBook();
	}

	public static void createXML() {
		ArrayList<Book> bookList = parseXML();
		// 生成XML
		// 1.创建一个SAXTransformerFactory 类的对象
		SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
		
		try {
			//2.通过 SAXTransformerFactory 对象创建一个 TransformerHandler 对象
			TransformerHandler handler = tff.newTransformerHandler();
			//3.通过 handler 对象创建一个 Transformer 对象
			Transformer tr = handler.getTransformer();
			//4.通过Transformer对象生成的xml文件进行设置
			//设置xml的编码
			tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			//设置xml的"是否换行"
			tr.setOutputProperty(OutputKeys.INDENT, "yes");
			File f = new File("src/main/resources/xmlfile/books2.xml");
			if (!f.exists()) {
				f.createNewFile();
			}
			//6.创建Result对象，并且使其与handler关联
			Result result = new StreamResult(new FileOutputStream(f));
			handler.setResult(result);
			//7.利用handler对象进行XML内容的编写
			//打开document
			handler.startDocument();
			AttributesImpl atts = new AttributesImpl();
			handler.startElement("", "", "bookstore", atts);
			for (Book book : bookList) {
				atts.clear();
				atts.addAttribute("", "", "id", "", book.getId());
				handler.startElement("", "", "book", atts);
				//创建name
				if (book.getName()!=null&&!book.getName().trim().equals("")) {
					atts.clear();
					handler.startElement("", "", "name", atts);
					handler.characters(book.getName().toCharArray(), 0, book.getName().length());
					handler.endElement("", "", "name");
				}
				
				
				//创建author
				if (book.getAuthor()!=null&&!book.getAuthor().trim().equals("")) {
					atts.clear();
					handler.startElement("", "", "author", atts);
					handler.characters(book.getAuthor().toCharArray(), 0, book.getAuthor().length());
					handler.endElement("", "", "author");
				}
				
				//创建year
				if (book.getYear()!=null&&!book.getYear().trim().equals("")) {
					atts.clear();
					handler.startElement("", "", "year", atts);
					handler.characters(book.getYear().toCharArray(), 0, book.getYear().length());
					handler.endElement("", "", "year");
				}
				
				//创建price
				if (book.getPrice()!=null&&!book.getPrice().trim().equals("")) {
					atts.clear();
					handler.startElement("", "", "price", atts);
					handler.characters(book.getPrice().toCharArray(), 0, book.getPrice().length());
					handler.endElement("", "", "price");
				}
				
				//创建language
				if (book.getLanguage()!=null&&!book.getLanguage().trim().equals("")) {
					atts.clear();
					handler.startElement("", "", "language", atts);
					handler.characters(book.getLanguage().toCharArray(), 0, book.getLanguage().length());
					handler.endElement("", "", "language");
				}
				
				handler.endElement("", "", "book");				
			}
			handler.endElement("", "", "bookstore");
			
			//关闭document
			handler.endDocument();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
	
}


















